local love = require 'love'

local push = love.graphics.push
local pop = love.graphics.pop
local translate = love.graphics.translate
local rotate = love.graphics.rotate
local setColor = love.graphics.setColor
local drawRectangle = love.graphics.rectangle
local arc = love.graphics.arc
local setLineWidth = love.graphics.setLineWidth
local line = love.graphics.line

local width = Width
local padding = Padding

-- 重来按钮使用蓝色
local restartButton = {
    x = 0,
    y = 0,
    width = 25,
    height = 0,
    normalColor = {0.129, 0.588, 0.953, 1.0}, -- #2196F3(天空蓝)
    hoveredColor = {0.259, 0.647, 0.961, 1.0}, -- #42A5F5(较浅的蓝色)
    pressedColor = {0.1, 0.463, 0.839, 1.0}, -- #1976D2(较深的蓝色)
    currentColor = {},
    isHovered = false,
    isPressed = false,
    iconX = 0,
    iconY = 0,
    iconSize = 0,
    iconRotation = 0,
    rotationSpeed = 0,
    radius = 0,
    lineWidth = 0,
    arcAngle = 0,
    capLength = 0,
    angleOffset = 0,
}

local function init()
    restartButton.x = width - restartButton.width - padding
    restartButton.y = padding
    restartButton.width = restartButton.width
    restartButton.height = restartButton.width

    restartButton.currentColor = restartButton.normalColor

    restartButton.iconX = restartButton.x + restartButton.width/2
    restartButton.iconY = restartButton.y + restartButton.height/2
    restartButton.iconSize = restartButton.height * 0.6
    restartButton.iconRotation = restartButton.iconRotation -- 旋转角度

    restartButton.radius = restartButton.iconSize * 0.45 -- 圆弧半径
    restartButton.lineWidth = restartButton.iconSize * 0.12 -- 线条宽度
    restartButton.arcAngle = math.rad(270) -- 圆弧角度（270度）
    restartButton.capLength = restartButton.radius * 0.5 -- 垂直小线段的长度
    restartButton.angleOffset = math.rad(170) -- 与半径的夹角（45度）
end

-- 绘制带垂直小线段的旋转圆弧
local function drawRestartIcon()
    -- 保存当前坐标系
    push()

    -- 移动到中心并旋转
    translate(restartButton.iconX, restartButton.iconY)
    rotate(math.rad(restartButton.iconRotation))

    -- 设置线条样式
    setLineWidth(restartButton.lineWidth)
    setColor(0.1, 0.3, 0.5)

    -- 绘制圆弧（从顶部开始，顺时针旋转270度）
    local startAngle = math.rad(-90)  -- 起始角度（向上）
    local endAngle = startAngle + restartButton.arcAngle  -- 结束角度

    arc("line", "open", 0, 0, restartButton.radius, startAngle, endAngle)

    -- 计算圆弧末端点的坐标
    local endX = restartButton.radius * math.cos(endAngle)
    local endY = restartButton.radius * math.sin(endAngle)

    -- 计算与半径成45度角的方向
    local capAngle = endAngle - restartButton.angleOffset  -- 方向 - angleOffset 度

    -- 计算小线段的两个端点
    local capPoint = {
        x = endX + restartButton.capLength * math.cos(capAngle),
        y = endY + restartButton.capLength * math.sin(capAngle)
    }

    -- 绘制45度小线段（圆弧头部）
    setLineWidth(restartButton.lineWidth)

    line(endX, endY, capPoint.x, capPoint.y)

    -- 恢复坐标系
    pop()
end

local function drawButton()
    -- 更新按钮颜色
    if restartButton.isPressed then
        restartButton.currentColor = restartButton.pressedColor
    elseif restartButton.isHovered then
        restartButton.currentColor = restartButton.hoveredColor
    else
        restartButton.currentColor = restartButton.normalColor
    end

    -- 绘制按钮
    setColor(restartButton.currentColor)
    drawRectangle("fill", restartButton.x, restartButton.y, restartButton.width, restartButton.height, 10, 10)

    -- 绘制按钮边框
    setColor(restartButton.pressedColor)
    setLineWidth(1)
    drawRectangle("line", restartButton.x, restartButton.y, restartButton.width, restartButton.height, 10, 10)

    -- 绘制重新开始图标
    drawRestartIcon()
end

local function inArea(x, y)
    return x >= restartButton.x and x <= restartButton.x + restartButton.width and
           y >= restartButton.y and y <= restartButton.y + restartButton.height
end

local function update(dt)
    if restartButton.isHovered then
        -- 悬停时缓慢旋转
        restartButton.rotationSpeed = 45 * dt -- 每秒旋转45度

        -- 更新旋转角度
        restartButton.iconRotation = restartButton.iconRotation + restartButton.rotationSpeed
        if restartButton.iconRotation >= 360 then
            restartButton.iconRotation = restartButton.iconRotation - 360
        end
    else
        -- 重置旋转速度
        restartButton.rotationSpeed = 0
    end
end

local function isHovered(boolean)
    restartButton.isHovered = boolean
end

local function isPressed()
    restartButton.isPressed = true
end

local function isReleased()
    restartButton.isPressed = false
end

return{
    init = init,
    drawButton = drawButton,
    inArea = inArea,
    update = update,
    isHovered = isHovered,
    isPressed = isPressed,
    isReleased = isReleased
}